<template>
  <div class="review-container">
    <h2>服务评价</h2>
    <el-card class="review-card">
      <el-form :model="reviewForm" :rules="rules" ref="reviewForm" label-width="100px">
        <el-form-item label="服务评分" prop="rating">
          <el-rate v-model="reviewForm.rating" :colors="colors"></el-rate>
        </el-form-item>
        <el-form-item label="评价内容" prop="content">
          <el-input
            type="textarea"
            v-model="reviewForm.content"
            :rows="4"
            placeholder="请输入您的评价内容"
          ></el-input>
        </el-form-item>
        <el-form-item label="上传图片">
          <el-upload
            action="#"
            list-type="picture-card"
            :auto-upload="false"
            :on-change="handleImageChange"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitReview">提交评价</el-button>
          <el-button @click="goBack">返回</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'ReviewPage',
  data() {
    return {
      reviewForm: {
        rating: 5,
        content: '',
        images: []
      },
      rules: {
        rating: [
          { required: true, message: '请选择服务评分', trigger: 'change' }
        ],
        content: [
          { required: true, message: '请输入评价内容', trigger: 'blur' },
          { min: 10, message: '评价内容不能少于10个字符', trigger: 'blur' }
        ]
      },
      colors: ['#99A9BF', '#F7BA2A', '#FF9900']
    }
  },
  methods: {
    handleImageChange(file) {
      this.reviewForm.images.push(file)
    },
    async submitReview() {
      try {
        await this.$refs.reviewForm.validate()
        // 调用提交评价API
        // await this.$http.post(`/api/orders/${this.$route.params.id}/review`, this.reviewForm)
        this.$message.success('评价提交成功')
        this.$router.push('/orders')
      } catch (error) {
        if (error !== 'cancel') {
          this.$message.error('评价提交失败')
        }
      }
    },
    goBack() {
      this.$router.back()
    }
  }
}
</script>

<style scoped>
.review-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.review-container h2 {
  margin-bottom: 30px;
  color: #333;
}

.review-card {
  padding: 20px;
}

.el-rate {
  margin-top: 8px;
}
</style> 